package com.vm.osm.business.controller;

import com.vm.osm.business.common.ControllerExceptionHandler;
import com.vm.osm.business.dto.req.failOrder.ReqAddFailOrder;
import com.vm.osm.business.dto.req.failOrder.ReqDispatchFail;
import com.vm.osm.business.dto.req.failOrder.ReqSearchFailureOrder;
import com.vm.osm.business.dto.rsp.failOrder.Priority;
import com.vm.osm.business.dto.rsp.failOrder.RspFailOrderDetail;
import com.vm.osm.business.dto.rsp.failOrder.RspFailOrderPage;
import com.vm.osm.business.entity.OsmOrderFailure;
import com.vm.osm.business.entity.OsmOrderFlow;
import com.vm.osm.business.service.*;
import com.vm.osm.business.util.UserDataUtil;
import com.vm.osm.business.util.UuidUtil;
import com.vma.eams.spring.boot.annotation.LogFunc;
import com.vma.eams.spring.boot.annotation.LogModule;
import io.swagger.annotations.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by chenzui on 2018/1/26.
 */
@RestController
@RequestMapping("/v1.0")
@LogModule(type = "order",name = "工单管理")
@Api(value = "FailOrderController",description = "故障单",tags = {"FailOrderController"})
public class FailOrderController extends ControllerExceptionHandler {
    private Logger LOG = Logger.getLogger(this.getClass());

    @Autowired
    private IOrderFailureService orderFailureService;

    @Autowired
    private RestTemplate restTemplate;

    @Value("${eam_api_url}")
    private String eamApiUrl;

    @Value("${omp_api_url}")
    private String ompApiUrl;

    @Autowired
    private IOsmPriorityService osmPriorityService;

    /**
     * 查询故障单详情
     * @return
     */
    @ApiOperation(value = "查询故障单详情",notes = "查询故障单详情")
    @RequestMapping(value = "/get_detail/{id}",method = RequestMethod.GET)
    public RspFailOrderDetail getOrderDetail(
            @ApiParam(required=true, name="id", value="工单ID") @PathVariable("id") String id){
        LOG.info("查询故障单详情");
        return orderFailureService.getOrderFailureById(id);
    }



    @ApiOperation(value = "获取故障单列表信息",notes = "获取故障单列表信息")
    @ApiImplicitParam(name = "reqSearchFailureOrder",value = "更新设备类型请求参数",required = true,dataType = "ReqSearchFailureOrder")
    @RequestMapping(value = "/fail_orders",method = RequestMethod.POST)
    public RspFailOrderPage getOrderPage(HttpServletRequest request,@RequestBody ReqSearchFailureOrder reqSearchFailureOrder){

        LOG.info("获取故障单列表信息");
        String staffId = UserDataUtil.getStaffId(request);
        reqSearchFailureOrder.setStaff_id(staffId);
        reqSearchFailureOrder.setEnterprise_id(UserDataUtil.getUserSelectedEnterpriseId(request));
        return orderFailureService.queryOrderFailureByCond(reqSearchFailureOrder);
    }

    @ApiOperation(value = "获取故障单列表信息",notes = "获取故障单列表信息")
    @ApiImplicitParam(name = "reqSearchFailureOrder",value = "更新设备类型请求参数",required = true,dataType = "ReqSearchFailureOrder")
    @RequestMapping(value = "/fail/department",method = RequestMethod.POST)
    public RspFailOrderPage getDepartmentOrder(HttpServletRequest request,@RequestBody ReqSearchFailureOrder reqSearchFailureOrder){

        LOG.info("获取故障单列表信息");
        reqSearchFailureOrder.setDepartment_id(UserDataUtil.getDepartmentId(request));
        reqSearchFailureOrder.setEnterprise_id(UserDataUtil.getUserSelectedEnterpriseId(request));
        return orderFailureService.queryOrderFailureByCond(reqSearchFailureOrder);
    }

    @ApiOperation(value = "获取当前登录员工发起的故障单",notes = "获取故障单列表信息")
    @RequestMapping(value = "/fail/orders_login_staff",method = RequestMethod.POST)
    public RspFailOrderPage queryOrders(HttpServletRequest request,
                                        @ApiParam(name = "page_num",value = "page_num",required = true) @RequestParam("page_num") Integer pageNum,
                                        @ApiParam(name = "page_size",value = "page_size",required = true) @RequestParam("page_size") Integer pageSize){

        LOG.info("获取故障单列表信息");
        String staffId = UserDataUtil.getStaffId(request);
        return orderFailureService.queryOrderFailureByStaffId(pageNum,pageSize,staffId);
    }

    /**
     * 撤销、确认、回退、删除
     * @param id
     * @param status
     * @param remark
     */
    @ApiOperation(value = "工单操作",notes = "撤销、确认、回退、删除,故障单状态说明" +
            "0草稿1审批中2待分配3处理中4处理完成5已完成6已关闭7已退回8故障上报9待受理10撤回")
    @RequestMapping(value = "/handle_order",method = RequestMethod.PUT)
    public void handleOrderStatus(
            HttpServletRequest request,
            @ApiParam(required=true, name="id", value="工单ID") @RequestParam("id") String id,
            @ApiParam(required=true, name="status", value="状态") @RequestParam("status") String status,
            @ApiParam(required=false, name="remark", value="备注") @RequestParam(value = "remark",required = false) String remark){
        LOG.info("工单操作");
        String staffId = UserDataUtil.getStaffId(request);
        String staffName = UserDataUtil.getStaffName(request);
        orderFailureService.handleOrder(id,status,remark,staffName,staffId);
    }

    /**
     * 派单
     */
    @LogFunc(type = "dispatch",name = "指派工单")
    @ApiOperation(value = "派单",notes = "派单")
    @ApiImplicitParam(name = "reqDispatchFail",value = "派单参数",required = true,dataType = "ReqDispatchFail")
    @RequestMapping(value = "/dispatch",method = RequestMethod.PUT)
    public void dispatch(@RequestBody ReqDispatchFail reqDispatchFail){
        LOG.info("派单");
        orderFailureService.dispatchIn(reqDispatchFail);
    }

    @ApiOperation(value = "提交故障工单",notes = "提交故障工单")
    @LogFunc(type = "commit",name = "提交故障工单")
    @ApiImplicitParam(name = "reqAddFailOrder",value = "提交故障工单参数",required = true,dataType = "ReqAddFailOrder")
    @RequestMapping(value = "/failure/commit",method = RequestMethod.POST)
    public String commit(@RequestBody ReqAddFailOrder reqAddFailOrder, HttpServletRequest request){
        LOG.info("提交故障工单");
        return orderFailureService.commit(reqAddFailOrder,request);
    }
    @LogFunc(type = "commit",name = "创建工单")
    @ApiOperation(value = "特殊途径提交故障工单",notes = "特殊途径提交故障工单，跳过审核")
    @ApiImplicitParam(name = "reqAddFailOrder",value = "提交故障工单参数",required = true,dataType = "ReqAddFailOrder")
    @RequestMapping(value = "/failure/special_commit",method = RequestMethod.POST)
    public void specialCommit(@RequestBody ReqAddFailOrder reqAddFailOrder, HttpServletRequest request){
        LOG.info("特殊途径提交故障工单");
        orderFailureService.specialCommit(reqAddFailOrder,request);
    }

    @ApiOperation(value = "删除故障工单",notes = "删除故障工单")
    @RequestMapping(value = "/failure/delete",method = RequestMethod.DELETE)
    public void delete(@ApiParam(required=true, name="idArr", value="故障单ID数组") @RequestParam("idArr") String[] idArr){
        LOG.info("删除故障工单");
//         TODO: 2018/3/20 校验故障单状态是否可以删除
        for(int i=0;i<idArr.length;i++){
            orderFailureService.deleteByPrimaryKey(idArr[i]);
        }
    }
    @ApiOperation(value = "获取设备详情",notes = "获取设备详情,详细接口文档参考http://120.55.12.229:9001/eam/swagger-ui.html#!/EquInfoController/getEquipmentDetailUsingGET")
    @RequestMapping(value = "/failure/device_info/{id}",method = RequestMethod.GET)
    public Object getDeviceInfoById(@ApiParam(required=true, name="id", value="设备编码") @PathVariable("id") String id){
        String url =  eamApiUrl+"/eam/v1.0/equipments/"+id;
        ResponseEntity entity = restTemplate.getForEntity(url,Object.class);
        return entity.getBody();
    }

    @ApiModelProperty(value = "优先级数据",name = "优先级数据")
    @RequestMapping(value = "/failure/get_all_priority",method = RequestMethod.GET)
    public Object getAll(HttpServletRequest request){
        String url = eamApiUrl+"/eam/v1.0/"+UserDataUtil.getUserSelectedEnterpriseId(request)+"/response_set";
        return restTemplate.getForEntity(url,Object.class).getBody();
    }


}
